package deliver_order

import (
	"fmt"
	Reids "github.com/felixrobcoding/go-common/redis"
	"maya-service/store/redis"
	"time"
)

// OrderCenter 订单中心
type OrderCenter struct {
}

func NewOrderCenter() *OrderCenter {
	return &OrderCenter{}
}

// GrabOrder 抢订单
func (o *OrderCenter) GrabOrder(userId string, orderId string) error {
	th := Reids.TRedLock{}
	bSuccess, err := th.Lock(redis.KV, "grab_order_"+orderId, time.Second*10)
	if err != nil {
		return fmt.Errorf("很遗憾，您没有抢到订单，请稍后再试")
	}
	if !bSuccess {
		return fmt.Errorf("很遗憾，您没有抢到订单，请稍后再试")
	}
	defer th.UnLock(redis.KV)

	// 查询订单状态是否是待抢状态
	// 抢单成功
	// 执行订单
	go func() {
		_ = o.EffectOrder(orderId)
	}()
	return nil
}

// EffectOrder 生效订单
func (o *OrderCenter) EffectOrder(orderId string) error {
	return nil
}

// 生成订单
func (o *OrderCenter) buildOrder() error {
	return nil
}
